//
//  HeaderView.m
//  IOS-7day
//
//  Created by RIOS on 2018/5/3.
//  Copyright © 2018年 RIOS. All rights reserved.
//

#import "HeaderView.h"

@interface HeaderView () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *vScrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *vPageControl;
@property (nonatomic,strong) NSMutableArray *marray;
@property (weak,nonatomic) NSTimer *timer;
@end

@implementation HeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (instancetype)buildView{
    return [[[NSBundle mainBundle] loadNibNamed:@"headerView" owner:nil options:nil]firstObject];
}

-(void)awakeFromNib{
    [super awakeFromNib];
    self.marray = [[NSMutableArray alloc]init];
    [self.marray addObject:@"icon1"];
    [self.marray addObject:@"icon2"];
    [self.marray addObject:@"icon3"];
    [self.marray addObject:@"icon4"];
    [self.marray addObject:@"icon5"];
    NSLog(@"%@",self.marray);
    if (self.marray != nil) {
        [self setScrollView];
    }
}

-(void)setScrollView{
    
    int count = (int)self.marray.count;
    self.vPageControl.numberOfPages = count;
    self.vPageControl.currentPage = 0;

    int width = self.vScrollView.frame.size.width;
    int height = self.vScrollView.frame.size.height;
  
    self.vScrollView.contentSize = CGSizeMake(width*count, 0);
    self.vScrollView.showsVerticalScrollIndicator = false;
    self.vScrollView.showsHorizontalScrollIndicator = false;
    self.vScrollView.pagingEnabled = true;
    self.vScrollView.delegate = self;
  
    
    for (int i= 0; i < count; i++) {
        UIImageView *vImageView = [UIImageView new];
        vImageView.frame = CGRectMake(i*width, 0, width, height);
        vImageView.contentMode = UIViewContentModeScaleAspectFill;
        vImageView.clipsToBounds = true;
        NSString *imageName = self.marray[i];
        [vImageView setImage:[UIImage imageNamed:imageName]];
        [self.vScrollView addSubview:vImageView];
        
    }
    [self createTimer];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int offsetX = scrollView.contentOffset.x;
     int width = self.vScrollView.frame.size.width;
    int page = (offsetX+ width/2)/(width) ;
    self.vPageControl.currentPage = page;
}

-(void)autoScroll{
    NSLog(@"滚动了autoScroll");
    int count = (int)self.marray.count;
    int width = self.vScrollView.frame.size.width;
    int offsetX = self.vScrollView.contentOffset.x;
    int page = (offsetX+ width/2)/(width) + 1 ;
    if ( page >= count ) {
        page = 0;
    }
    [self.vScrollView setContentOffset:CGPointMake(page*width, 0)  animated:true];
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"开始拖动了");
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"结束拖动");
    [self createTimer];
}


-(void)createTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(autoScroll) userInfo:nil repeats:true];
    NSRunLoop *loop = [NSRunLoop currentRunLoop];
    [loop addTimer:self.timer forMode:NSRunLoopCommonModes];
}




@end
